package cn.kys.modules.system.service.impl;


import cn.kys.modules.system.repository.ToolsResposity;
import cn.kys.modules.system.service.FormService;
import cn.kys.modules.system.service.GetDeptIdByLoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Primary
@Service
public class FormServiceImplzxw implements FormService {

    @Autowired
    private GetDeptIdByLoginUser getDeptIdByLoginUser;
    @Value("${adminUserIncludeStr}")
    private String adminUserIncludeStr;

    // 加部门权限的id
    public Long getDeptId() {
        return getDeptIdByLoginUser.getDeptId();
    }

    // 加部门权限的用户名
    public String getUserName() {
        return getDeptIdByLoginUser.getUserName();
    }


    @Autowired
    private ToolsResposity toolsResposity;

    @Override
    public Map addForm(String name) {
        Map returnMap = new HashMap();
//        String sql = "SELECT * FROM public.t_form where name = '" + name + "'";
        String sql = String.format("SELECT * FROM public.t_form where name = '%s' and dept_id=%d", name, getDeptId());
        Map formMap = toolsResposity.exeSql(sql);
        if (formMap == null) {
//            String sql_insert = "INSERT INTO public.t_form(name) VALUES ('" + name + "');";
            String sql_insert = String.format("INSERT INTO public.t_form(name,dept_id) VALUES ('%s',%d)", name, getDeptId());
            toolsResposity.exeSql(sql_insert);
            returnMap.put("success", true);
        } else {
            returnMap.put("success", false);
            returnMap.put("msg", "表单项目已经存在");
        }

        return returnMap;
    }

    @Override
    public Map editForm(String id, String name, String formcontentids) {
        Map resultMap = new HashMap();

        List<Map> nameList = toolsResposity.queryNameFromForm();
        for (Map map : nameList) {
            //判断更新的是不是同一条，是的话
            String idStr = map.get("id").toString();
            String nameStr = map.get("name").toString();
            if (!id.equals(idStr) && name.equals(nameStr)) {
                resultMap.put("success", false);
                resultMap.put("msg", name + "已经存在");
                return resultMap;
            }
        }

        //如果名字已经存在，更新
        String sql = "UPDATE t_form SET name = '" + name + "' WHERE id ='" + id + "'";
        toolsResposity.exeSql(sql);

        //更新t_form_content表,先删除，再插入

        //删除
        String sql_delete = "delete from t_form_content where formid =" + id;
        toolsResposity.exeSql(sql_delete);
        //更新
        if (!"".equals(formcontentids)) {
            String formcontentidArr[] = formcontentids.split(",");
            for (int i = 0; i < formcontentidArr.length; i++) {
                String formcontentid = formcontentidArr[i];
                String sql_formcontent = "select * from t_formcontent where id = " + formcontentid;
                Map formcontentMap = toolsResposity.exeSql(sql_formcontent);
                if (formcontentMap != null) {
                    String formcontentname = formcontentMap.get("name").toString();
                    String formcontentcname = formcontentMap.get("cname").toString();
                    String type = formcontentMap.get("type").toString();
                    int ordernum = i + 1;
                    String sql_insert = "INSERT INTO public.t_form_content(formid,formcontentid,formname,formcontentname,formcontentcname,type,is_show,ordernum) VALUES (" + id + ",'" + formcontentid + "','" + name + "','" + formcontentname + "','" + formcontentcname + "','" + type + "'," + false + ',' + ordernum + ");";
                    toolsResposity.exeSql(sql_insert);
                }
            }
        }
        resultMap.put("success", true);
        resultMap.put("msg", "编辑成功");
        return resultMap;
    }

    @Override
    public void updateFormContentShowStatus(String ids) {
        //更新
        if (!"".equals(ids)) {
            String idArr[] = ids.split(",");
            for (int i = 0; i < idArr.length; i++) {
                String id = idArr[i];
                String sql = "UPDATE t_form_content SET is_show = " + true + " WHERE id ='" + id + "'";
                toolsResposity.exeSql(sql);
            }
        }
    }

    @Override
    public List getAllForms() {

        String sql = "";
        if (getUserName().contains(adminUserIncludeStr)) {
            sql = String.format("SELECT * FROM public.t_form");
        } else {
            sql = String.format("SELECT * FROM public.t_form where dept_id=%d", getDeptId());
        }
        return toolsResposity.exeSqlList(sql);

    }


    @Override
    public Map delFormById(String id) {

        Map returnMap = new HashMap();
        List<Map> caseFormList = toolsResposity.queryCaseFormByFormid(Integer.parseInt(id));
        if (caseFormList.size() > 0) {
            returnMap.put("deleted", false);
            returnMap.put("resMsg", "和方案表关联，不能删除");
        } else {
            //可以删除
            String sql_delete = "delete from t_form where id in (" + id + ")";
            toolsResposity.exeSql(sql_delete);
            String sql_form_content_delete = "delete from t_form_content where formid in (" + id + ")";
            toolsResposity.exeSql(sql_form_content_delete);
            returnMap.put("deleted", true);
            returnMap.put("resMsg", "可以删除");
        }
        return returnMap;
    }

    @Override
    public List getBindingByFormId(String id) {
        String sql = "SELECT * FROM public.t_form_content where formid = " + id + " order by ordernum";
        return toolsResposity.exeSqlList(sql);
    }
}

